from selenium.webdriver.common.by import By
from base.base import Base
from util.number_util import get_all_number


class Page_Site(Base):

    topnav_locator = (By.XPATH,'(//li[contains(@class,"ant-menu-overflow-item")])[2]')
    input_seach_locator = (By.XPATH,'//input[@placeholder="站点搜索"]')
    err_info_locator = (By.XPATH,'(//p[@class="ant-empty-description"])[1]')
    site_loctor = (By.XPATH,'//div[@class="ant-tree-list-holder-inner"]//span[contains(text(),"xq")]')  #搜索内容
    top_data_locator = (By.CLASS_NAME,'monitor-count-card-num')
    alarm_list_locator = (By.XPATH,'//td[contains(@class,"ant-table-cell")]')
    morealarm_locator = (By.CLASS_NAME,'more-btn')
    devicelist_locator =(By.CLASS_NAME,'item-info')
    devicetable_btn_locator = (By.XPATH,'//span[text()="表格"]')
    devicetable_locator = (By.XPATH,'(//div[@class="ant-row"])[2]//td[@class="ant-table-cell"]')


    def page_jump(self):
        self.click(self.topnav_locator)

    def input_site(self,sitename):
        self.input(self.input_seach_locator,sitename)

    def get_err_info(self):
        return self.get_text(self.err_info_locator)
    
    def click_site(self):
        self.click(self.site_loctor)

    def get_topdata(self):
        values = []
        for elm in self.finds(self.top_data_locator):
            values.append(self.get_text_elm(elm))
        return get_all_number(values)
    
    def get_alarmlist(self):
        values = []
        for elm in self.finds(self.alarm_list_locator):
            values.append(self.get_text_elm(elm))
        return values
    
    def click_morealarm(self):
        self.click(self.morealarm_locator)

    def get_devicelist(self):
        values = []
        devicelist = []
        for elm in self.finds(self.devicelist_locator):
            values.append(self.get_text_elm(elm).split('\n'))
        for elms in values:
            for index,elm in enumerate(elms):
                if index == 0:
                    devicelist.append(elm)
                else:
                    devicelist.append(elm.split('：')[1].replace('℃',''))
        return devicelist
    
    def click_devicetable(self):
        self.click(self.devicetable_btn_locator)

    def get_devicetable(self):
        values = []
        for index,elm in enumerate(self.finds(self.devicetable_locator)):
            if  index%5 == 4:
                values.append(self.get_text_elm(elm).replace(' ℃',''))
            else:
                values.append(self.get_text_elm(elm))
        return values